package com.xyzboom.compiler.util

fun String.lineIndexThatIndexOf(index: Int): Pair<Int, Int> {
    var now = 0
    var line = 0
    val lines = this.lines()
    val lineEndLen =
        if (contains("\r\n"))
            2
        else 1
    var indexInLine = 0
    for (s in lines) {
        now += s.length + lineEndLen
        line++
        if (now >= index || line > lines.size) {
            indexInLine = index - now + s.length + lineEndLen
            break
        }
    }
    return Pair(line, indexInLine - 1)
}

fun String.generateRandom(len: Int): String {
    val sb = StringBuilder()
    for (j in 0..len) {
        sb.append(random())
    }
    return sb.toString()
}